package com.zl.customer.web;

import com.zl.customer.service.CustomerService;
import com.zl.customer.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author suke
 * @create 2021-07-09 16:16
 */
@WebServlet(name = "DeleteCustomerServlet",urlPatterns = "/delCustomer.action")
public class DeleteCustomerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String cid = request.getParameter("cid");
        //把String 转换int     Double.parseDouble()
        int id =  Integer.parseInt(cid);

        CustomerService cutsomerService = new CustomerServiceImpl();
        int rs = 0;
        try {
            rs = cutsomerService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(rs > 0){ //添加成功
            request.setAttribute("msg","客户删除成功");
        }else{ //添加成功
            request.setAttribute("msg","客户删除失败,请联系管理员!!");
        }
        //跳转到msg.jsp   使用request域, 只能使用转发     错误信息 操作信息 不推荐保存到session
        request.getRequestDispatcher("/jsp/msg.jsp").forward(request,response);
    }
}
